package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.entity.Role;
import com.qk.mapper.RoleMapper;
import com.qk.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public PageResult<Role> selectlist(String name, String label, Integer page, Integer pageSize) {
        //设置分页参数
        PageHelper.startPage(page, pageSize);
        //调用接口方法进行查询
        List<Role> list = roleMapper.selectlist(name, label);
        //解析分页结果
        Page<Role> pageData = (Page<Role>)list;
        // 封装成PageResult
        return new PageResult<>(pageData.getTotal(), pageData.getResult());
    }

    @Override
    public void delete(Integer id) {
        roleMapper.delete(id);
    }

    @Override
    public void add(Role role) {
        role.setCreateTime(LocalDateTime.now());
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.insert(role);
    }

    @Override
    public Role getById(Integer id) {
        return roleMapper.selectById(id);
    }

    @Override
    public void update(Role role) {
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.update(role);
    }

    //查询所有角色
    @Override
    public List<Role> list() {
        return roleMapper.list();
    }
}
